package com.csdj.server.exception;

import com.csdj.server.pojo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author zyq
 * @Date 2022/12/27 10:21
 * @ClassName: GlobalExceptionHandler
 * @Description: TODO 全局异常处理类
 * @Version 1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕捉的 SQLException 异常
     * @param e
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public RespBean mySQLException(SQLException e){

        if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联的数据,操作失败!");
        }
        return RespBean.error("数据库异常,操作失败!");
    }


}
